import sys,os

def run_salsa(ref, fq1, fq2, outdir):
    cmd = f"/scratch/project/qaafi-cnafs/jingping/software/miniconda3/bin/bowtie2-build {ref} contigs -p 50\n"
    cmd += f"samtools faidx {ref}\n"
    cmd += f"/scratch/project/qaafi-cnafs/jingping/software/miniconda3/bin/bowtie2 -p 50 -x contigs -1 {fq1} -2 {fq2} | samtools view -S -b > align.bam\n"
    cmd += f"/scratch/project/qaafi-cnafs/jingping/software/miniconda3/bin/bamToBed -i align.bam > align.bed\n"
    cmd += f"module load salsa2/2.3\n"
    cmd += f"run_pipeline.py -a {ref} -l {ref}.fai -b align.bed -o {outdir} -e TTAA,GTAC --clean yes --exp 877\n"
    slum = open("salsa2.slum", "w")
    slum.write('''#!/bin/bash --login
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=1
#SBATCH --cpus-per-task=50
#SBATCH --mem=80G
#SBATCH --job-name=salsa2
#SBATCH --time=160:00:00
#SBATCH --partition=general
#SBATCH --account=a_qaafi_genomics
#SBATCH -o juicer.slurm.out
#SBATCH -e juicer.slurm.error
#module perl
#module load salsa/2.3\n''')
    slum.write(cmd)
    slum.close()
    os.system(f"sbatch salsa2.slum")
    

try:
    ref = sys.argv[1]
    fq1 = sys.argv[2]
    fq2 = sys.argv[3] 
    outdir = sys.argv[4]
    run_salsa(ref, fq1, fq2, outdir)
except:
    print("usage: python salsa2.py reference fq1 fq2....")
    